Repository navigation
Fix Dict.has evaluating its key before its dict - #9544
Conversation
Why:
`Dict.has(d, k)` compiles to `k in d`, and JS evaluates the key
first. ReScript evaluates arguments left to right, so the effects of
the two arguments ran in the wrong order. Fixes #9222.
When the order can be seen, the dict is now evaluated first, into a
temporary. The output stays `k in d` when either argument is a constant
or a variable nothing assigns, or when neither has side effects.
JS modified (Dict.has whose arguments must run in order):
Example:
let eff: (string, 'a) => 'a = %raw(`function (s, v) { log.push(s); return v }`)
let bothEffects = (d: dict<int>, k) => Dict.has(eff("dict", d), eff("key", k))
let dicts = ref(dict{"a": 1})
let dictReadsState = (other: dict<int>) =>
Dict.has(
dicts.contents,
{
dicts := other
eff("key", "a")
},
)
Before (js):
function bothEffects(d, k) {
return eff("key", k) in eff("dict", d);
}
function dictReadsState(other) {
return (dicts.contents = other, eff("key", "a")) in dicts.contents;
}
After (js):
function bothEffects(d, k) {
let tmp = eff("dict", d);
return eff("key", k) in tmp;
}
function dictReadsState(other) {
let tmp = dicts.contents;
return (dicts.contents = other, eff("key", "a")) in tmp;
}
JS modified (Dict.has whose order cannot be seen):
Example:
let constantKey = (d: dict<int>) => eff("dict", d)->Dict.has("a")
let variableDict = (d: dict<int>) => d->Dict.has(eff("key", "a"))
let variables = (d: dict<int>, k) => d->Dict.has(k)
Before (js): unchanged
function constantKey(d) {
return "a" in eff("dict", d);
}
function variableDict(d) {
return eff("key", "a") in d;
}
function variables(d, k) {
return k in d;
}
After (js): unchanged
function constantKey(d) {
return "a" in eff("dict", d);
}
function variableDict(d) {
return eff("key", "a") in d;
}
function variables(d, k) {
return k in d;
}
Review: Why in JS compilation and not in Lambda? Call arguments are already held there in order, with `delay_safe` and `evaluate_now`. `hold_dict_has` reuses them. It shares the new `order_matters` with `hold_obj_args`, which made the same check for the optional fields of an `@obj` external.
Review: Can a getter still see the key read first? Yes. Index reads count as pure, as for `@obj` fields, so `Dict.has(o["x"], o["y"])` keeps `o["y"] in o["x"]` and gets no temporary. This is deliberate.
Review: What about the conditional key from the issue's comment? #9428 fixed it: `d->Dict.has(c ? "a" : "b")` prints `(c ? "a" : "b") in d`, and `printer_operand_levels_test` covers it.
Review: Does other output change? No. The runtime, Belt and every test's JavaScript compile as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq
Signed-off-by: Christoph Knittel <ck@cca.io>
Changelog🐛 Bug fix
Semantic Diff1 commit · Full page Fix
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #9544 +/- ##
=======================================
Coverage 82.68% 82.68%
=======================================
Files 469 469
Lines 66582 66588 +6
=======================================
+ Hits 55052 55058 +6
Misses 11530 11530
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
✅ Compiler benchmarksAllocated wordsIn total: 656,461,005 → 656,461,766 (±0.00%). Generated projects: 628,526,307 → 628,527,875 (±0.00%). None of the 265 benchmarks changed by 0.1% or more. InstructionsIn total: 8,976,406,297 → 8,976,462,256 (±0.00%). None of the 33 benchmarks changed by 0.1% or more. Output sizeIn total: 27,594,926 → 27,595,129 (±0.00%). None of the 13 benchmarks changed by 0.1% or more. Pull request e8d8557 compared with base daa4f2d in this run. |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9544 |
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
cristianoc
left a comment
There was a problem hiding this comment.
The regression tests cover ordinary observable effects and mutable operands. The documented getter/Proxy case remains intentionally outside this fix, consistent with the existing purity model for property reads.
Fixes #9222.
Dict.has(d, k)compiles tok in d, and JavaScript evaluates the key first. ReScript evaluates arguments left to right, so when both arguments had side effects, they ran in the wrong order.Now, when the order can be observed, the dict is evaluated first, into a temporary:
The output stays
k in dwhen either argument is a constant or a variable nothing assigns, or when neither has side effects. Sod->Dict.has("a"),d->Dict.has(k)andeff(d)->Dict.has("a")are unchanged, and no existing test output changes.How
The fix is in the JavaScript compilation, where call arguments are already held in order (
delay_safe,evaluate_now). The check is a neworder_mattershelper, shared withhold_obj_args, which made the same check for the optional fields of an@objexternal.Index reads count as pure here, as they do for
@objfields. A getter or a Proxy can therefore still see the key read first:Dict.has(o["x"], o["y"])keepso["y"] in o["x"], without a temporary. This is deliberate.The conditional key from the issue's comment was already fixed by #9428.
Test
dict_has_order_testlogs the effects of both arguments and checks their order. It covers:tryas the key;Dict.has;The order tests fail on master.
🤖 Generated with Claude Code
https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq